package main

import (
	"github.com/labstack/echo"
	"github.com/labstack/echo/middleware"
	"github.com/robfig/cron"
	"src/config"
	newmid "src/middleware"
	"src/model"
	"src/view"
	"time"
)

func main() {
	go startTimer()
	startinitroute()
	startWeb()
}

func startinitroute() {
	hour := time.Now().Hour()
	if hour >= 18 {
		model.ChangeRouteIn18()
	}
}

func startWeb() {
	e := echo.New()
	e.Use(middleware.Logger())
	e.Use(middleware.Recover())

	v1 := e.Group("/api/v1")

	NotskipperPaths := []string{
		"/api/v1/notice/action/add",
		"/api/v1/notice/action/change",
		"/api/v1/notice/action/delete",
		"/api/v1/lostproperty/action",
		"/api/v1/bus/action/routechange",
	}
	v1.Use(middleware.JWTWithConfig(newmid.CustomJWTConfig(NotskipperPaths, "Bearer")))

	login := v1.Group("/login")
	view.Login(login)

	bus := v1.Group("/bus")
	view.BusView(bus)

	stations := v1.Group("/station")
	view.Station(stations)

	routes := v1.Group("/route")
	view.Route(routes)

	line := v1.Group("/line")
	view.Line(line)

	notice := v1.Group("/notice")
	view.Notice(notice)

	lostproperty := v1.Group("/lostproperty")
	view.LostProperty(lostproperty)

	test := v1.Group("/test")
	view.Test(test)

	visitor := v1.Group("/visitor")
	view.Visitor(visitor)

	_ = e.Start(":8080")
	println("启动")

}

func startTimer() {
	c := cron.New()
	_ = c.AddFunc(config.DefaultCronTime, model.SetAllBusState)       //定时任务
	_ = c.AddFunc(config.ChangeroutrCronTime1, model.ChangeRouteIn6)  //凌晨6点
	_ = c.AddFunc(config.ChangeroutrCronTime2, model.ChangeRouteIn18) //下午6点
	_ = c.AddFunc(config.ChangeroutrCronTime3, model.ChangeRouteEveryMonth)
	c.Start()
}
